<template>
  <div class="songs-container">
    <div class="tab-bar">
      <span class="item" :class="{active:tag==0}" @click="tag=0">全部</span>
      <span class="item" :class="{active:tag==7}" @click="tag=7">华语</span>
      <span class="item" :class="{active:tag==96}" @click="tag=96">欧美</span>
      <span class="item" :class="{active:tag==8}" @click="tag=8">日本</span>
      <span class="item" :class="{active:tag==16}" @click="tag=16">韩国</span>
    </div>
    <!-- 底部的table -->
    <table class="el-table playlit-table">
      <thead>
        <th></th>
        <th></th>
        <th>音乐标题</th>
        <th>歌手</th>
        <th>专辑</th>
        <th>时长</th>
      </thead>
      <tbody>
        <tr v-for="(item,index) in lists" :key="index" class="el-table__row">
          <td>{{index + 1}}</td>
          <td style="padding-bottom: 10px;">
            <div class="img-wrap">
              <img :src="item.album.picUrl" alt="" />
              <span @click="playMusic(item.id)" class="iconfont icon-play"></span>
            </div>
          </td>
          <td>
            <div class="song-wrap">
              <div class="name-wrap">
                <span>{{item.name}}</span>
                <span v-if="item.mvid" class="iconfont icon-MV" @click="tomv(item.mvid)"></span>
              </div>
              <span>{{item.album.name}}</span>
            </div>
          </td>
          <td>{{item.album.artists["0"].name}}</td>
          <td>{{item.album.name}}</td>
          <td>{{item.duration}}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import getSongs from '../utils/songs';
export default {
  name: 'songs',
  data() {
    return {
      lists:[],
      tag:0
    };
  },
  watch: {
    tag(){
      this.listsData();
    }
  },
  created() {
    this.listsData();
  },
  methods: {
    formatTime(time) {
      let min = parseInt(time/1000/60);
      let sec = parseInt(time/1000%60);
      if(min < 10) {
        min = '0' + min;
      } 
      if(sec < 10) {
        sec = '0' + sec;
      }
      return (min + ':' + sec);
    },
    listsData(){
      getSongs.getSongLists(this.tag).then(res => {
        // console.log(res.data.data)
        this.lists = res.data.data;
        for (let i = 0; i < this.lists.length; i++){
          let time = this.formatTime(this.lists[i].duration);
          this.lists[i].duration = time;
        }
    })
    },
    playMusic(id){
      getSongs.play(id).then(res => {
        // console.log(res.data.data[0].url);
        this.$parent.musicUrl = res.data.data[0].url;
      })
    },
    tomv(id){
      this.$router.push(`/mv?q=${id}`);
    }
  }
};
</script>

<style >

</style>
